package com.b2b.util;

import com.b2b.model.BiddingDto;
import com.b2b.model.BiddingRecordDto;
import com.b2b.model.entity.Bidding;
import com.b2b.model.entity.BiddingRecord;
import com.b2b.model.receive.BriefGood;
import com.b2b.model.receive.MyBidding;
import com.b2b.model.receive.MyBiddingRecord;
import com.b2b.model.receive.Record;
import com.b2b.service.BiddingRecordService;
import com.b2b.service.BiddingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class BiddingUtil {
    private final BiddingService biddingService;
    private final BiddingRecordService biddingRecordService;
    private final GoodsUtil goodsUtil;
    private final CompanyInfoUtil companyInfoUtil;
    @Autowired
    public BiddingUtil(BiddingService biddingService,BiddingRecordService biddingRecordService,GoodsUtil goodsUtil,
                       CompanyInfoUtil companyInfoUtil){
        this.biddingService = biddingService;
        this.biddingRecordService = biddingRecordService;
        this.goodsUtil = goodsUtil;
        this.companyInfoUtil = companyInfoUtil;
    }

    public Integer insertBidding(Bidding bidding){
        Integer count = biddingService.insert(bidding);
        if (count >= 0){
            count = bidding.getBiddingId();
        }
        return count;
    }

    public List<BiddingDto> getBiddingList(){
        List<BiddingDto> biddingDtoList = new ArrayList<>();
        List<Bidding> biddingList = biddingService.selectAll();
        for (Bidding value:biddingList){
            BiddingDto biddingDto = getBiddingDto(value);
            biddingDtoList.add(biddingDto);
        }
        return biddingDtoList;
    }
    public Integer updateBidding(Bidding bidding){
        Bidding biddingTemp = new Bidding();
        biddingTemp.setBiddingId(bidding.getBiddingId());
        biddingTemp.setState(bidding.getState());
        Integer count = biddingService.updateByPrimaryKeySelective(bidding);

        return count;
    }
    public Integer insertBiddingRecord(BiddingRecord biddingRecord){
        Integer biddingId = biddingRecord.getBiddingId();
        Bidding bidding = new Bidding();
        bidding.setBiddingId(biddingId);
        bidding = biddingService.selectByPrimaryKey(bidding);
        bidding.setCurrentPrice(biddingRecord.getBiddingPrice());
        Integer result = biddingService.updateByPrimaryKeySelective(bidding);
        if (result <= 0){
            return result;
        }else {
            biddingRecord.setBiddingTime(new Date());
            Integer count = biddingRecordService.insert(biddingRecord);
            return count;
        }
    }


    public List<BiddingDto> getAll(){
        List<BiddingDto> biddingDtoList = new ArrayList<>();
        Bidding bidding = new Bidding();
        char state = '1';
        bidding.setState(state);
        List<Bidding> biddingList = biddingService.select(bidding);
        for (Bidding value:biddingList){
            BiddingDto biddingDto = getBiddingDto(value);
            biddingDtoList.add(biddingDto);
        }
        return biddingDtoList;
    }

    public List<BiddingDto> getMyBidding(Integer companyId){
        List<BiddingDto> biddingDtoList = new ArrayList<>();
        Bidding bidding = new Bidding();
        bidding.setCompanyId(companyId);
        List<Bidding> biddingList = biddingService.select(bidding);
        for (Bidding value : biddingList) {
            BiddingDto biddingDto = getBiddingDto(value);
            biddingDtoList.add(biddingDto);
        }
        return biddingDtoList;
    }

    public Bidding getBidding(Integer biddingId){
        Bidding bidding = biddingService.selectByPrimaryKey(biddingId);
        return bidding;
    }
    public List<BiddingRecordDto> getBiddingRecord(Integer biddingId){
        List<BiddingRecordDto> biddingRecordDtoList = new ArrayList<>();
        BiddingRecord biddingRecord = new BiddingRecord();
        biddingRecord.setBiddingId(biddingId);
        List<BiddingRecord> biddingRecords = biddingRecordService.select(biddingRecord);
        for (BiddingRecord value : biddingRecords){
            BiddingRecordDto biddingRecordDto = getBiddingRecordDto(value);
            biddingRecordDtoList.add(biddingRecordDto);
        }
        return biddingRecordDtoList;
    }
    public BiddingRecordDto getBiddingRecordDto(BiddingRecord biddingRecord){
        BiddingRecordDto biddingRecordDto = new BiddingRecordDto();
        biddingRecordDto.setBiddingId(biddingRecord.getBiddingId());
        biddingRecordDto.setBiddingTime(biddingRecord.getBiddingTime());
        biddingRecordDto.setPrice(biddingRecord.getBiddingPrice());

        char[] companyId = String.valueOf(biddingRecord.getCompanyId()).toCharArray();
        char[] newCompanyId = new char[companyId.length];
        for (int i = 0; i < 2; i++) {
            newCompanyId[i] = companyId[i];
        }
        for (int i = 2; i < companyId.length-2; i++) {
            newCompanyId[i] = '*';
        }
        for (int i = companyId.length-2; i < companyId.length; i++) {
            newCompanyId[i] = companyId[i];
        }
        String companyIdS = String.copyValueOf(newCompanyId);
        biddingRecordDto.setCompanyId(companyIdS);

        return biddingRecordDto;
    }
    public List<MyBiddingRecord> getBiddingRecordList(Integer companyId){
        BiddingRecord biddingRecord = new BiddingRecord();
        biddingRecord.setCompanyId(companyId);
        List<BiddingRecord> biddingRecords = biddingRecordService.select(biddingRecord);
        List<MyBiddingRecord> myBiddingRecords = new ArrayList<MyBiddingRecord>();
        for (int i = 0; i < biddingRecords.size(); i++) {
            BiddingRecord biddingRecord1 = biddingRecords.get(i);

            Bidding bidding = getBiddingById(biddingRecord1.getBiddingId());

            MyBiddingRecord myBiddingRecord = new MyBiddingRecord();
            myBiddingRecord.setBiddingId(bidding.getBiddingId());
            myBiddingRecord.setBiddingPrice(biddingRecord1.getBiddingPrice());
            myBiddingRecord.setBiddingDeadline(bidding.getBiddingDeadline());
            myBiddingRecord.setCurrentPrice(bidding.getCurrentPrice());
            myBiddingRecord.setState(bidding.getState());
            myBiddingRecord.setGoodsName(goodsUtil.getGoodNameByS(bidding.getStockId()));

            myBiddingRecords.add(myBiddingRecord);
        }
        return myBiddingRecords;
    }

    public Bidding getBiddingById(Integer biddingId){
        Bidding bidding = new Bidding();
        bidding.setBiddingId(biddingId);
        bidding = biddingService.selectByPrimaryKey(bidding);
        return bidding;
    }

    public BiddingDto getBiddingDto(Bidding bidding){
        BiddingDto biddingDto = new BiddingDto();
        BiddingRecord biddingRecord = new BiddingRecord();
        biddingRecord.setBiddingId(bidding.getBiddingId());
        Integer stockId = bidding.getStockId();
        BriefGood briefGood = goodsUtil.getBriefGoodByS(stockId);
        biddingDto.setBiddingId(bidding.getBiddingId());
        biddingDto.setGoodsName(briefGood.getGoodsName());
        biddingDto.setGoodsImgUrl(briefGood.getGoodsImgUrlList().get(0));
        biddingDto.setGoodsId(briefGood.getGoodsId());
        biddingDto.setBiddingDeadline(bidding.getBiddingDeadline());
        biddingDto.setReservePrice(bidding.getReservePrice());
        biddingDto.setCurrentPrice(bidding.getCurrentPrice());
        biddingDto.setQuotationNumber(biddingRecordService.selectCount(biddingRecord));
        biddingDto.setState(bidding.getState());

        return biddingDto;
    }

    public List<MyBidding> getMyBidding(List<BiddingDto> biddingDtoList){
        List<MyBidding> myBiddingList = new ArrayList<>();
        for (int i = 0; i < biddingDtoList.size(); i++) {
            MyBidding myBidding = new MyBidding();
            myBidding.setBiddingId(biddingDtoList.get(i).getBiddingId());
            myBidding.setGoodsName(biddingDtoList.get(i).getGoodsName());
            myBidding.setCurrentPrice(biddingDtoList.get(i).getCurrentPrice());
            myBidding.setState(biddingDtoList.get(i).getState());
            myBidding.setRecordList(getRecord(biddingDtoList.get(i).getBiddingId()));
            myBiddingList.add(myBidding);
        }
        return myBiddingList;
    }

    public List<Record> getRecord(Integer biddingId){
        List<Record> recordList = new ArrayList<>();
        BiddingRecord biddingRecord = new BiddingRecord();
        biddingRecord.setBiddingId(biddingId);
        PageHelper.startPage(1, 5);
        PageHelper.orderBy("bidding_price" + " " + "desc");
        List<BiddingRecord> biddingRecordList = biddingRecordService.select(biddingRecord);
        PageInfo<BiddingRecord> selectPage = new PageInfo<BiddingRecord>(biddingRecordList);
        for (int i = 0; i < selectPage.getList().size(); i++) {
            Record record = new Record();
            record.setBiddingPrice(selectPage.getList().get(i).getBiddingPrice());
            record.setCompanyId(selectPage.getList().get(i).getCompanyId());
            record.setCompanyName(companyInfoUtil.getCompanyName(selectPage.getList().get(i).getCompanyId()));
            recordList.add(record);
        }
        return recordList;
    }
}
